package com.oumiao.echarts.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

@Service
@Slf4j
public class RabbitMqConsumerService {

    private final String queueName = "exchange.fanout.realtimedata.prps";
    private final String exchangeName = "exchange.fanout.realtimedata";

    @Bean
    public Queue messageQueue() {
        return new Queue(queueName,false,false,true);
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(exchangeName,false,false);
    }

    @Bean
    public Binding binding(Queue messageQueue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(messageQueue).to(fanoutExchange);
    }

    @RabbitListener(queues = queueName)
    public void handleMessage(Message message) {
        String messageContent = new String(message.getBody(), StandardCharsets.UTF_8);
        log.debug("接收内容 {}",messageContent);
    }

}
